"""

@Time: 2021/9/13 14:41
@Author:
@File: school_service.py
"""
from fastapi import Depends
from dependencies import get_mysql_db
from sqlalchemy.orm import Session
from typing import List
from models.po.mysql_po import Volunteer as VolPo
from models.dto.vol_dto import Volunteer as VolDto, VolQueryCond


class VolService:
    def __init__(self, mysql: Session = Depends(get_mysql_db)):
        self.mysql = mysql

    def __del__(self):
        self.mysql.close()

    def query_volunteer(self, cond: VolQueryCond) -> List[VolDto]:
        q = self.mysql.query(VolPo)
        if cond.province:
            q = q.filter(VolPo.province == cond.province)
        if cond.is_985 is not None:
            q = q.filter(VolPo.is_985 == cond.is_985)
        if cond.is_211 is not None:
            q = q.filter(VolPo.is_211 == cond.is_211)
        if cond.low_score is not None:
            q = q.filter(VolPo.lowest_score >= cond.low_score)
        if cond.high_score is not None:
            q = q.filter(VolPo.lowest_score <= cond.high_score)
        if cond.major:
            q = q.filter(VolPo.professional_name.like(f'%{cond.major}%'))
        result: List[VolPo] = q.all()
        volunteers = [VolDto.from_mysql_schema(v) for v in result]
        return volunteers

